package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduOrder;
import com.jf3q.common.utils.JwtTokenUtil;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 */
@RestController
@RequestMapping("/order")
@Tag(name="订单controller")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private JwtTokenUtil jwtUtils;

    //查看我的订单
    @GetMapping("/my")
    @Operation(summary = "分页查看我的订单")
    public ResultVo my(HttpServletRequest request, @RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = Integer.MAX_VALUE+"") Integer pageSize){

        String token = request.getHeader("token");

        if(!StringUtils.hasText(token)){
            throw new RuntimeException("token必传");
        }
        String userId = jwtUtils.getUserIdByToken(token);
        LambdaQueryWrapper<EduOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduOrder::getMemberId,userId);
        queryWrapper.orderByDesc(EduOrder::getEduCreate);
        return ResultVo.success(orderService.page(new Page<>(pageNum,pageSize), queryWrapper));

    }

    @PostMapping("/createOrder/{courseId}")
    @Operation(summary = "创建订单")
    public ResultVo saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId, jwtUtils.getUserIdByToken(request.getHeader("token")));
        return ResultVo.success(orderNo);
    }

    @GetMapping("/getOrderInfo/{orderId}")
    @Operation(summary = "根据订单id查询订单信息")
    public ResultVo getOrderInfo(@PathVariable String orderId) {
        LambdaQueryWrapper<EduOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduOrder::getOrderNo,orderId);
        EduOrder order = orderService.getOne(queryWrapper);
        return ResultVo.success(order);
    }
}
